package zhihui.mobile.ui.shop.category;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zgm.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import cn.lankton.flowlayout.FlowLayout;
import zhihui.mobile.R;
import zhihui.mobile.bean.shop.category.CategoryInfo;


/**
 * Created by 张公铭 on 2017/8/1 0001.
 */

public class CategoryAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<CategoryInfo> mList = new ArrayList<>();
    private ICategoryOnClickListener listener;


    public interface ICategoryOnClickListener
    {
        void goShopList(int categoryId);
    }

    public void setListener(ICategoryOnClickListener listener)
    {
        this.listener = listener;
    }

    public CategoryAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setCategoryList(List<CategoryInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_name;
        FlowLayout fl_context;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_category_content, null);
            tv_name = (TextView) convertView.findViewById(R.id.tv_categort_name);
            fl_context = (FlowLayout) convertView.findViewById(R.id.fl_category_content);

            ViewCache cache = new ViewCache();
            cache.tv_name = tv_name;
            cache.fl_context = fl_context;

            convertView.setTag(cache);
        } else
        {
            ViewCache cache = (ViewCache) convertView.getTag();
            tv_name = cache.tv_name;
            fl_context = cache.fl_context;
        }

        if (mList != null && mList.get(position) != null)
        {
            CategoryInfo categoryInfo = mList.get(position);
            tv_name.setText(categoryInfo.getName());
            fl_context.removeAllViews();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(DensityUtil.dip2px(mContext, 10), 0, DensityUtil.dip2px(mContext, 10), 0);
            for (final CategoryInfo temp : mList.get(position).getChild())
            {
                final TextView tv_item = new TextView(mContext);
                tv_item.setText(temp.getName());
                tv_item.setTextSize(16);
                tv_item.setLayoutParams(layoutParams);
                tv_item.setPadding(DensityUtil.dip2px(mContext, 15), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 15), DensityUtil.dip2px(mContext, 5));
                tv_item.setGravity(Gravity.CENTER);
                tv_item.setBackgroundResource(R.drawable.category_item_click);
                tv_item.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.goShopList(temp.getId());
                    }
                });
                fl_context.addView(tv_item);
            }

        }
        return convertView;
    }

    class ViewCache
    {
        public TextView tv_name;
        public FlowLayout fl_context;
    }

    class ItemAdapter extends BaseAdapter
    {
        private Context mContext;
        private List<CategoryInfo> mList = new ArrayList<>();
        private LayoutInflater inflater;

        public ItemAdapter(Context context, List<CategoryInfo> list)
        {
            mContext = context;
            inflater = LayoutInflater.from(mContext);
            mList.clear();
            mList.addAll(list);
        }

        @Override
        public int getCount()
        {
            return mList.size();
        }

        @Override
        public Object getItem(int position)
        {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return mList.get(position).getId();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            TextView tv_name;
            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.category_item, null);
                tv_name = (TextView) convertView.findViewById(R.id.tv_category_item_name);

                ItemViewCache cache = new ItemViewCache();
                cache.tv_name = tv_name;

                convertView.setTag(cache);
            } else
            {
                ItemViewCache cache = (ItemViewCache) convertView.getTag();
                tv_name = cache.tv_name;
            }
            if (mList != null && mList.get(position) != null)
            {
                tv_name.setText(mList.get(position).getName());
                tv_name.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.goShopList(mList.get(position).getId());
                    }
                });
            }
            return convertView;
        }
    }

    class ItemViewCache
    {
        public TextView tv_name;
    }
}
